import { Injectable } from '@angular/core';
import { HttpResponse } from '@angular/common/http';
import { Resolve, ActivatedRouteSnapshot, RouterStateSnapshot, Routes } from '@angular/router';
import { UserRouteAccessService } from 'app/core';
import { of } from 'rxjs';
import { map } from 'rxjs/operators';
import { DeliverOffer } from 'app/shared/model/deliver-offer.model';
import { DeliverOfferService } from './deliver-offer.service';
import { DeliverOfferComponent } from './deliver-offer.component';
import { DeliverOfferDetailComponent } from './deliver-offer-detail.component';
import { DeliverOfferUpdateComponent } from './deliver-offer-update.component';
import { IDeliverOffer } from 'app/shared/model/deliver-offer.model';

@Injectable({ providedIn: 'root' })
export class DeliverOfferResolve implements Resolve<IDeliverOffer> {
    constructor(private service: DeliverOfferService) {}

    resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
        const id = route.params['id'] ? route.params['id'] : null;
        if (id) {
            return this.service.find(id).pipe(map((deliverOffer: HttpResponse<DeliverOffer>) => deliverOffer.body));
        }
        return of(new DeliverOffer());
    }
}

export const deliverOfferRoute: Routes = [
    {
        path: 'deliver-offer',
        component: DeliverOfferComponent,
        data: {
            authorities: ['ROLE_USER'],
            pageTitle: 'DeliverOffers'
        },
        canActivate: [UserRouteAccessService]
    },
    {
        path: 'deliver-offer/:id/view',
        component: DeliverOfferDetailComponent,
        resolve: {
            deliverOffer: DeliverOfferResolve
        },
        data: {
            authorities: ['ROLE_USER'],
            pageTitle: 'DeliverOffers'
        },
        canActivate: [UserRouteAccessService]
    },
    {
        path: 'deliver-offer/new',
        component: DeliverOfferUpdateComponent,
        resolve: {
            deliverOffer: DeliverOfferResolve
        },
        data: {
            authorities: ['ROLE_USER'],
            pageTitle: 'DeliverOffers'
        },
        canActivate: [UserRouteAccessService]
    },
    {
        path: 'deliver-offer/:id/edit',
        component: DeliverOfferUpdateComponent,
        resolve: {
            deliverOffer: DeliverOfferResolve
        },
        data: {
            authorities: ['ROLE_USER'],
            pageTitle: 'DeliverOffers'
        },
        canActivate: [UserRouteAccessService]
    }
];
